package agenda

class DayEvent(_agenda: Agenda, _day: Int, _subject: String, isOccupying: Boolean)
  extends Entry(_agenda, _day, _subject) with Occupiable {
  isOccupying_(isOccupying)

  override def isOccupying_ (isOccupying : Boolean) : Unit = {
    if (!isTerminated)
    	_isOccupying = isOccupying
  }

  override def getEndTime(): Long = getStartTime() + 24 * 60 * 60 * 1000

  override def overlapsWith(other: Entry): Boolean = {
    if ((other == null) || (!isOccupying))
      return false
    if (day != other.day)
      return false
    if (other.isInstanceOf[Occupiable])
      return other.asInstanceOf[Occupiable].isOccupying
    return true
  }

  override def mergeWith(other: Entry): DayEvent = {
    require(other != null)
    require(day == other.day)
    require((!isTerminated()) && (!other.isTerminated()))
    require(agenda == other.agenda)
    require(other.isInstanceOf[DayEvent])
    val otherDayEvent: DayEvent = other.asInstanceOf[DayEvent]
    this.subject = this.subject + other.subject
    isOccupying_(isOccupying || otherDayEvent.isOccupying)
    other.terminate()
    return this
  }
}